package com.gwsdk.paysdk.acitvityV2.fragment;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.gwsdk.paysdk.adapter.GWAccountManagerAdapter;
import com.gwsdk.paysdk.bean.PersonalCenterModel;
import com.gwsdk.paysdk.bean.SdkDomain;
import com.gwsdk.paysdk.callback.AddAccountCallback;
import com.gwsdk.paysdk.common.Constant;
import com.gwsdk.paysdk.dialog.AddAccountDialog;
import com.gwsdk.paysdk.dialog.GWTipDialog;
import com.gwsdk.paysdk.entity.UserLogin;
import com.gwsdk.paysdk.http.process.AddSmallAccountProgress;
import com.gwsdk.paysdk.http.process.SmallAccountListProgress;
import com.gwsdk.paysdk.listener.OnMultiClickListener;
import com.gwsdk.paysdk.utils.AppStatusManager;
import com.gwsdk.paysdk.utils.GWInflaterUtils;
import com.gwsdk.paysdk.utils.TextUtils;
import com.gwsdk.paysdk.utils.ToastUtil;

import java.util.List;

public class GWSmallAccountFragment extends GWBaseFragment{

    private ImageView backItem;

    private GWAccountManagerAdapter adapter;
    private GWTipDialog mcTipDialog;

    private ListView listView;
    private View progressView;
    private View layoutHaveData;
    private TextView btnAddAccount;  //添加小号按钮
    private List<UserLogin.SmallAccountEntity> smallAccountList;
    private AddAccountDialog.addAccountBuilder dialogBuilder;
    @Override
    public int setContentView() {
        return GWInflaterUtils.getLayout(getActivity(),"gw_fragment_small_account");
    }

    @Override
    public void initView(View view) {

        backItem = view.findViewById(GWInflaterUtils.getControl(getActivity(),"btn_back"));
        backItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        btnAddAccount = view.findViewById(GWInflaterUtils.getControl(getActivity(),"add_account"));

        layoutHaveData = view.findViewById(GWInflaterUtils.getControl(getActivity(),"layout_haveData"));

        progressView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"progress_view"));

        adapter = new GWAccountManagerAdapter(getActivity());

        listView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"list_view"));
        listView.setAdapter(adapter);

        //添加小号按钮点击事件
        btnAddAccount.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                if (smallAccountList.size()>=10){
                    ToastUtil.show(getActivity(),"小号个数已满10，不可继续添加");
                }else {
                    dialogBuilder = new AddAccountDialog.addAccountBuilder().setSureClick(clickcallback);
                    dialogBuilder.show(getActivity(), getFragmentManager());
                }
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                UserLogin.SmallAccountEntity entity = smallAccountList.get(position);
                String smallId = entity.getSmallUserId();
                String lastLoginSmallId = PersonalCenterModel.getInstance().getSmallAccountUserID();
                if (!PersonalCenterModel.getInstance().getSmallAccountUserID().equals(smallId)){
                    AppStatusManager.getInstance().smallAccountLogin(smallId,getFragmentManager());
                    getActivity().finish();
                }
            }
        });
    }

    @Override
    public void getData() {
        SmallAccountListProgress progress = new SmallAccountListProgress();
        progress.post(mHandler);
    }

    /**
     * 添加小号弹窗确认按钮点击事件
     */
    AddAccountCallback clickcallback = new AddAccountCallback() {
        @Override
        public void addAccount(String Account) {
            if (TextUtils.isEmpty(Account)){
                ToastUtil.show(getActivity(),"小号名不能为空");
                return;
            }
            dialogBuilder.closeDialog(getFragmentManager());
            mcTipDialog = new GWTipDialog.Builder().setMessage("请稍等...").show(getActivity(),getFragmentManager());
            AddSmallAccountProgress addSmallAccountProgress = new AddSmallAccountProgress();
            addSmallAccountProgress.setSmaleNickname(Account.trim());
            addSmallAccountProgress.setUserId(PersonalCenterModel.getInstance().getUserId());
            addSmallAccountProgress.setGameId(SdkDomain.getInstance().getGameId());
            addSmallAccountProgress.post(mHandler);
        }
    };



    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            progressView.setVisibility(View.GONE);
            layoutHaveData.setVisibility(View.VISIBLE);
            super.handleMessage(msg);
            if (null != mcTipDialog) {
                mcTipDialog.dismiss();
            }
            switch (msg.what) {
                case Constant.SMALLACCOUNT_LIST_SUCCESS: //获取小号列表数据成功
                    smallAccountList = (List<UserLogin.SmallAccountEntity>) msg.obj;
                    adapter.setListData(smallAccountList);
                    break;
                case Constant.SMALLACCOUNT_LIST_FAIL:
                    ToastUtil.show(getActivity(), msg.obj.toString());
                    break;
                case Constant.ADD_SMALL_ACCOUNT_SUCCESS:  //添加小号成功
                    UserLogin login = (UserLogin)msg.obj;
                    smallAccountList = login.getSmallAccountList();
                    adapter.setListData(smallAccountList);
                    ToastUtil.show(getActivity(), "添加成功");
                    break;
                case Constant.ADD_SMALL_ACCOUNT_FAIL:  //添加小号失败
                    String tip = (String) msg.obj;
                    ToastUtil.show(getActivity(), tip);
                    break;
                default:
                    break;
            }
        }
    };
}
